package com.tools.ad.strategy;

import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;
import android.text.format.DateUtils;

import com.cpu.time.AppRunTimeManager;
import com.hm.adsbuisiness.bean.HmAdPlace;
import com.hm.base.android.mob.AMApplication;
import com.hm.base.android.mob.util.network.NetWorkInfoParser;
import com.tools.ad.ADConstant;
import com.tools.ad.ADModule;
import com.tools.ad.log.MLog;
import com.tools.ad.util.TrueUserSwitchUtil;

/**
 * desc: 广告策略的执行者
 * date: 2017/12/20
 * author: ancun
 */

public abstract class AStrategyExecutor implements IStrategyExecutor {

    protected AMApplication imContext;
    protected ADModule mAdModule;
    protected String mAdScene;

    public AStrategyExecutor(AMApplication context, ADModule adModule, String adScene) {
        imContext = context;
        mAdModule = adModule;
        mAdScene = adScene;
    }

    protected abstract String getTag();

    private String getLastShowTimeKey() {
        return mAdScene + ADConstant.AdSharedPrefKey.SHOW_TIME;
    }

    private String getShowCountKey() {
        return mAdScene + ADConstant.AdSharedPrefKey.SHOW_COUNT;
    }

    private String getBeginTimeOfDayKey() {
        return mAdScene + ADConstant.AdSharedPrefKey.BEGIN_TIME;
    }

    private long getLastShowTime() {
        return mAdModule.getADBeanManager().getADSharedPrefManager().getLastShowTime(getLastShowTimeKey());
    }

    private void setLastShowTime(long lastShowTime) {
        mAdModule.getADBeanManager().getADSharedPrefManager().setLastShowTime(getLastShowTimeKey(), lastShowTime);
    }

    private int getShowCounts() {
        return mAdModule.getADBeanManager().getADSharedPrefManager().getShowCounts(getShowCountKey());
    }

    private void setShowCounts(int count) {
        mAdModule.getADBeanManager().getADSharedPrefManager().setShowCounts(getShowCountKey(), count);
    }

    private long getBeginTimeOfDay() {
        return mAdModule.getADBeanManager().getADSharedPrefManager().getBeginTimeOfDay(getBeginTimeOfDayKey());
    }

    private void setBeginTimeOfDay(long beginTime) {
        mAdModule.getADBeanManager().getADSharedPrefManager().setBeginTimeOfDay(getBeginTimeOfDayKey(), beginTime);
    }

    @Override
    public void reset() {
        //第一次或超过24小时，重置 开始时间和广告计数
        if (System.currentTimeMillis() - getBeginTimeOfDay() > DateUtils.DAY_IN_MILLIS) {
            setBeginTimeOfDay(System.currentTimeMillis());
            setShowCounts(0);
        }
    }

    @Override
    public void record() {
        if (System.currentTimeMillis() - getBeginTimeOfDay() > DateUtils.DAY_IN_MILLIS) {
            //第一次或超过24小时，重置 开始时间和广告计数
            setBeginTimeOfDay(System.currentTimeMillis());
            // 由于该方法有广告展示后调用，所以是 1
            setShowCounts(1);
        } else {
            //记录24小时内的展示次数
            setShowCounts(getShowCounts() + 1);
        }
        //记录当前最后一次展示的时间
        setLastShowTime(System.currentTimeMillis());
    }

    @Override
    public boolean check(HmAdPlace hmNativeAd) {
        //每次检查前 调用一次 reset();
        reset();

        // 判断是否有网
        if (!NetWorkInfoParser.isNetConnect(imContext)) {
            if (MLog.debug) {
                MLog.e(getTag(), "没网");
            }
            return false;
        }

        // 判断是否亮屏
        PowerManager pm = (PowerManager) imContext.getSystemService(Context.POWER_SERVICE);
        KeyguardManager manager = (KeyguardManager) imContext.getSystemService(Context.KEYGUARD_SERVICE);
        if (!pm.isScreenOn() || manager.isKeyguardLocked()) {
            if (MLog.debug) {
                MLog.e(getTag(), "暗屏");
            }
            return false;
        }

        if (hmNativeAd == null) {
            if (MLog.debug) {
                MLog.e(getTag(), "hmNativeAd null");
            }
            return false;
        }

        boolean on = hmNativeAd.getSwitch();
        if (!TrueUserSwitchUtil.checkSwitch(on)) {
            if (MLog.debug) {
                MLog.e(getTag(), "开关为关");
            }
            return false;
        }

        //检查配置
        int protect = hmNativeAd.getProTime();
        if (protect * DateUtils.HOUR_IN_MILLIS > AppRunTimeManager.getInstance().getAppUsedTime()) {
            if (MLog.debug) {
                MLog.e(getTag(), "check 保护时间内");
            }
            return false;
        }

        if (getLastShowTime() == 0) {
            if (MLog.debug) {
                MLog.e(getTag(), "check 第一次准备弹广告");
            }
            return true;
        }

        float interval = hmNativeAd.getInterval();
        if ((System.currentTimeMillis() - getLastShowTime() < (long) (interval * DateUtils.HOUR_IN_MILLIS))) {
            if (MLog.debug) {
                MLog.e(getTag(), "check 间隔时间内");
            }
            return false;
        }

        int count = hmNativeAd.getLimit();
        if (getShowCounts() >= count) {
            if (MLog.debug) {
                MLog.e(getTag(), "check 超过次数");
            }
            onCountLimited();
            return false;
        }

        if (MLog.debug) {
            MLog.e(getTag(), "通过配置及环境检查");
        }

        return true;
    }

    protected void onCountLimited() {

    }
}
